// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

var TVKey = require('ToolUtil').TVKey;
const tool = require('ToolUtil');
cc.Class({
    extends: cc.Component,
    properties: {
        roomNums: {
            default: [],
            type: [cc.Label]
        },
        keyNums: {
            default: [],
            type: [cc.Node]
        },
        btnClose: cc.Button, //helo
        btnEnter: cc.Button,
        btnCancel: cc.Button,
        btnNoPwd: cc.Button,
        lbMsg: cc.Node,
        inputSelctNode: cc.Node,
        keyBordNode: cc.Node,
        selectNode: cc.Node,
        inputNode: cc.Node,
        title: cc.Toggle,
        _roomPosArr: null,
        _isSelectRoom: null,
        _index: 0,
        _selectIdx: 0,
    },

    setUINode(node) {
        this._itemNode = node;
        this.setBuild(node.getComponent('WindowBuild'));
    },

    setBuild(build) {
        this._build = build;
    },

    getBuild() {
        return this._build;
    },

    show(data) {
        this.btnClose.getComponent('KeyView').clearFocuse();
        this.btnEnter.getComponent('KeyView').clearFocuse();
        this.btnCancel.getComponent('KeyView').clearFocuse();
        this.btnNoPwd.getComponent('KeyView').clearFocuse();
        if (data == 'enter') {
            this.title.isChecked = false;
            this._isSelectRoom = true;
            this.keyBordNode.active = true;
            this.inputNode.active = true;
            this.lbMsg.active = false;
            this.btnNoPwd.node.active = false;
            this.btnCancel.node.active = true;
            for (var i = 0; i < this.roomNums.length; i++) {
                this.roomNums[i].string = '';
            }
        } else {
            this.title.isChecked = true;
            this._isSelectRoom = false;
            this.keyBordNode.active = false;
            this.inputNode.active = false;
            this.lbMsg.active = true;
            this.btnNoPwd.node.active = true;
            this.btnCancel.node.active = false;
            this.btnNoPwd.getComponent('KeyView').onFocuse();
            //分割数字

            if (typeof (data) == 'string') {
                if (data.length == this.roomNums.length) {
                    for (var i = 0; i < this.roomNums.length; i++) {
                        this.roomNums[i].string = data[i];
                    }
                }

            }

        }
    },

    onLoad() {
        this._roomPosArr = [];
        this._isSelectRoom = true;
        this.keyBordNode.active = true;
        if (this.inputNode) {
            for (var i = 0; i < this.inputNode.childrenCount; i++) {
                if (this.inputNode.children[i]) {
                    if (this.inputNode.children[i] != this.selectNode) {
                        this._roomPosArr.push(this.inputNode.children[i].x);
                    }
                }
            }
        }
    },

    dismiss() {
        cc.windowManager.dismissWindow(this);
    },

    clearNums() {
        for (var i = 0; i < this.roomNums.length; i++) {
            this.roomNums[i].string = '';
        }
        this._index = 0;
        this.selectNode.x = this._roomPosArr[this._index];
    },

    onKeyPressed(keyCode) {
        switch (keyCode) {
            case cc.KEY.up:
            case TVKey.up:
                {
                    if (this._isSelectRoom) {
                        //向上选中closeRoom
                        this.btnClose.getComponent('KeyView').onFocuse();
                        this._isSelectRoom = false;
                        this.selectNode.active = false;
                        this.keyBordNode.active = false;
                    } else if (this.btnEnter.getComponent('KeyView').isFocuse()) {
                        this.btnEnter.getComponent('KeyView').clearFocuse();
                        if (this.inputNode.active) {
                            this._isSelectRoom = true;
                            this.selectNode.active = true;
                            this.keyBordNode.active = true;
                        } else {
                            this.btnClose.getComponent('KeyView').onFocuse();
                        }
                    } else if (this.btnCancel.getComponent('KeyView').isFocuse()) {
                        this.btnCancel.getComponent('KeyView').clearFocuse();
                        if (this.inputNode.active) {
                            this._isSelectRoom = true;
                            this.selectNode.active = true;
                            this.keyBordNode.active = true;
                        } else {
                            this.btnClose.getComponent('KeyView').onFocuse();
                        }

                    } else if (this.btnNoPwd.getComponent('KeyView').isFocuse()) {
                        this.btnNoPwd.getComponent('KeyView').clearFocuse();
                        this.btnClose.getComponent('KeyView').onFocuse();
                    }
                }
                break;
            case cc.KEY.down:
            case TVKey.down:
                {
                    if (this._isSelectRoom) {
                        //向上选中closeRoom
                        this.btnEnter.getComponent('KeyView').onFocuse();
                        this._isSelectRoom = false;
                        this.keyBordNode.active = false;
                        this.selectNode.active = false;
                    } else if (this.btnClose.getComponent('KeyView').isFocuse()) {
                        this.btnClose.getComponent('KeyView').clearFocuse();
                        if (this.inputNode.active) {
                            this._isSelectRoom = true;
                            this.keyBordNode.active = true;
                            this.selectNode.active = true;
                        } else {
                            this.btnEnter.getComponent('KeyView').onFocuse();
                        }
                    }
                }
                break;
            case cc.KEY.left:
            case TVKey.left:
                {
                    if (this.btnCancel.getComponent('KeyView').isFocuse()) {
                        this.btnCancel.getComponent('KeyView').clearFocuse();
                        this.btnEnter.getComponent('KeyView').onFocuse();
                    } else if (this.btnNoPwd.getComponent('KeyView').isFocuse()) {
                        this.btnNoPwd.getComponent('KeyView').clearFocuse();
                        this.btnEnter.getComponent('KeyView').onFocuse();
                    } else if (this._isSelectRoom) {
                        if (this._selectIdx > 0) {
                            this._selectIdx--;
                        } else {
                            this._selectIdx = this.keyNums.length - 1;
                        }
                        this.inputSelctNode.x = this.keyNums[this._selectIdx].x;
                    }
                }
                break;
            case cc.KEY.right:
            case TVKey.right:
                {
                    if (this.btnEnter.getComponent('KeyView').isFocuse()) {
                        this.btnEnter.getComponent('KeyView').clearFocuse();
                        if (this.btnNoPwd.node.active) {
                            this.btnNoPwd.getComponent('KeyView').onFocuse();
                        } else {
                            this.btnCancel.getComponent('KeyView').onFocuse();
                        }
                    } else if (this._isSelectRoom) {
                        if (this._selectIdx < this.keyNums.length - 1) {
                            this._selectIdx++;
                        } else {
                            this._selectIdx = 0;
                        }
                        this.inputSelctNode.x = this.keyNums[this._selectIdx].x;
                    }
                    // this.selectNode.x = this._roomPosArr[this._index++ % 6];
                }
                break;

            case cc.KEY.enter:
            case TVKey.enter:
                if (this._isSelectRoom) {
                    //是输入数字
                    var btn = this.keyNums[this._selectIdx].getComponent(cc.Button);
                    if (btn.clickEvents.length > 0) {
                        btn.clickEvents[0].emit([btn, btn.clickEvents[0].customEventData]);
                    }
                } else if (this.btnEnter.getComponent('KeyView').isFocuse()) {
                    this.btnEnter.getComponent('KeyView').onKeyEnter()
                } else if (this.btnCancel.getComponent('KeyView').isFocuse()) {
                    this.btnCancel.getComponent('KeyView').onKeyEnter()
                } else if (this.btnClose.getComponent('KeyView').isFocuse()) {
                    this.btnClose.getComponent('KeyView').onKeyEnter()
                } else if (this.btnNoPwd.getComponent('KeyView').isFocuse()) {
                    this.btnNoPwd.getComponent('KeyView').onKeyEnter()
                }
                break;
        }

    },

    onViewClickListener(sender, data) {
        // this.dismiss();
        cc.log(" click data =" + data);
        if (data == 'close') {
            this.dismiss();
        } else if (data == 'ok') {
            var roomId = ''
            for (var i = 0; i < this.roomNums.length; i++) {
                roomId += this.roomNums[i].string;
            }
            tool.request("enterTable", {
                tid: roomId
            }, function (data) {
                if (data.success) {
                    this.dismiss();
                    if (data.url) {
                        cc.windowManager.showWindowByNameWithDate('loadingwin', '加入房间中,请稍等...');
                        tool.NetManager().connect(data.url, data.gameid, function (state) {
                            if (state == 'connect') {
                                cc.windowManager.dismissWindowByName('loadingwin');
                                tool.NetManager().sendPacket('CSJSONROOM', {
                                    id: tool.PlayerManager().m_MySelf.m_dbid,
                                    tid: roomId,
                                    local: tool.getLocal()
                                });
                            }
                        }.bind(this));
                    }
                    return;
                } else {
                    cc.windowManager.dismissWindowByName('loadingwin');
                    this.clearNums();
                    if (data.errcode == 100) {
                        this.dismiss();
                        // showMessageBox(data.msg);
                        // SceneManager.changeScene(GameConfig.SCENE_LOGIN);
                    } else {
                        if (data.msg) {
                            tool.showMessageBox(data.msg);
                        }
                    }
                }
            }.bind(this), false);
            // this.dismiss();
        } else if (data == 'cancel') {
            this.dismiss();
        } else if (data == '0') {
            this.roomNums[this._index].string = '0';
            this._index++;
            if (this._index >= 6) {
                this._index = 5;
            } else {
                this.selectNode.x = this._roomPosArr[this._index];
            }
        } else if (data == '1') {
            this.roomNums[this._index].string = '1';
            this._index++;
            if (this._index >= 6) {
                this._index = 5;
            } else {
                this.selectNode.x = this._roomPosArr[this._index];
            }
        } else if (data == '2') {
            this.roomNums[this._index].string = '2';
            this._index++;
            if (this._index >= 6) {
                this._index = 5;
            } else {
                this.selectNode.x = this._roomPosArr[this._index];
            }
        } else if (data == '3') {
            this.roomNums[this._index].string = '3';
            this._index++;
            if (this._index >= 6) {
                this._index = 5;
            } else {
                this.selectNode.x = this._roomPosArr[this._index];
            }
        } else if (data == '4') {
            this.roomNums[this._index].string = '4';
            this._index++;
            if (this._index >= 6) {
                this._index = 5;
            } else {
                this.selectNode.x = this._roomPosArr[this._index];
            }
        } else if (data == '5') {
            this.roomNums[this._index].string = '5';
            this._index++;
            if (this._index >= 6) {
                this._index = 5;
            } else {
                this.selectNode.x = this._roomPosArr[this._index];
            }
        } else if (data == '6') {
            this.roomNums[this._index].string = '6';
            this._index++;
            if (this._index >= 6) {
                this._index = 5;
            } else {
                this.selectNode.x = this._roomPosArr[this._index];
            }
        } else if (data == '7') {
            this.roomNums[this._index].string = '7';
            this._index++;
            if (this._index >= 6) {
                this._index = 5;
            } else {
                this.selectNode.x = this._roomPosArr[this._index];
            }
        } else if (data == '8') {
            this.roomNums[this._index].string = '8';
            this._index++;
            if (this._index >= 6) {
                this._index = 5;
            } else {
                this.selectNode.x = this._roomPosArr[this._index];
            }
        } else if (data == '9') {
            this.roomNums[this._index].string = '9';
            this._index++;
            if (this._index >= 6) {
                this._index = 5;
            } else {
                this.selectNode.x = this._roomPosArr[this._index];
            }
        } else if (data == 'del') {
            if (this._index < 6)
                this.roomNums[this._index].string = '';
            this._index--;
            if (this._index <= 0) {
                this._index = 0;
            }
            this.selectNode.x = this._roomPosArr[this._index];
        }
    }

    // start () {

    // },

    // update (dt) {},
});